﻿using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Validate;

namespace Basic.FlowRemoteModel.Application.Model
{
    public class AuditRes
    {
        [NumValidate("flow.audit.item.id.error", 1)]
        public long ItemId
        {
            get;
            set;
        }
        /// <summary>
        /// 审批结果
        /// </summary>
        [EnumValidate("flow.audit.result.error", typeof(FlowAuditResult))]
        public FlowAuditResult AuditResult { get; set; }

        /// <summary>
        /// 回退的节点ID
        /// </summary>
        [EntrustValidate("_Check")]
        public long? BackNodeId { get; set; }

        /// <summary>
        /// 回退的批次号
        /// </summary>
        public long? BackBatchNo { get; set; }

        /// <summary>
        /// 意见
        /// </summary>
        [LenValidate("flow.audit.opinion.len", 0, 255)]
        public string Opinion { get; set; }

        /// <summary>
        /// 审核人员ID
        /// </summary>
        public long AuditEmpId { get; set; }

        /// <summary>
        /// 下一节点信息
        /// </summary>
        public ChioseAuditNode[] NextNode { get; set; }

        /// <summary>
        /// 是否恢复原节点
        /// </summary>
        public ChioseAuditNode Restore { get; set; }
        /// <summary>
        /// 签名文件
        /// </summary>
        public long? SignFileId { get; set; }

        /// <summary>
        /// 是否向前加签
        /// </summary>
        public bool? IsBefore { get; set; }
        /// <summary>
        /// 加签，委托，转交人
        /// </summary>
        public long? ToEmpId { get; set; }
        /// <summary>
        /// 附件列表
        /// </summary>
        public long[] FileId { get; set; }

        private static void _Check ( AuditRes res )
        {
            if ( res.AuditResult == FlowAuditResult.加签 || res.AuditResult == FlowAuditResult.委托 || res.AuditResult == FlowAuditResult.转办 )
            {
                if ( !res.ToEmpId.HasValue )
                {
                    throw new ErrorException("flow.audit.emp.null");
                }
            }
            if ( res.AuditResult == FlowAuditResult.加签 && !res.IsBefore.HasValue )
            {
                throw new ErrorException("flow.audit.is.before.null");
            }
            else if ( res.AuditResult == FlowAuditResult.回退 && ( res.BackNodeId.HasValue == false || res.BackBatchNo.HasValue == false ) )
            {
                throw new ErrorException("flow.audit.back.node.null");
            }
        }
    }
}
